home *** CD-ROM | disk | FTP | other *** search
/ Clickx 47 / Clickx 47.iso / assets / software / Miro_Installer.exe / Miro_Downloader.exe / guide.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2008-01-10  |  8.3 KB  |  272 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. import resources
  5. from database import DDBObject
  6. from template import fillStaticTemplate
  7. from httpclient import grabURL
  8. from urlparse import urlparse, urljoin
  9. from xhtmltools import urlencode
  10. from copy import copy
  11. from util import returnsUnicode, unicodify, checkU
  12. import re
  13. import app
  14. import config
  15. import indexes
  16. import menu
  17. import prefs
  18. import threading
  19. import urllib
  20. import eventloop
  21. import views
  22. import logging
  23. import httpclient
  24. from gtcache import gettext as _
  25. from HTMLParser import HTMLParser, HTMLParseError
  26. import iconcache
  27. HTMLPattern = re.compile('^.*(<head.*?>.*</body\\s*>)', re.S)
  28.  
  29. def isPartOfGuide(url, guideURL, allowedURLs = None):
  30.     '''Return if url is part of a channel guide where guideURL is the base URL
  31.     for that guide.
  32.     '''
  33.     if guideURL == '*':
  34.         return True
  35.     elif guideURL.startswith('file://'):
  36.         return False
  37.     elif allowedURLs is None:
  38.         guideHost = urlparse(guideURL)[1]
  39.         urlHost = urlparse(url)[1]
  40.         return urlHost.endswith(guideHost)
  41.     elif isPartOfGuide(url, guideURL):
  42.         return True
  43.     
  44.     for altURL in allowedURLs:
  45.         if isPartOfGuide(url, altURL):
  46.             return True
  47.             continue
  48.     
  49.     return False
  50.  
  51.  
  52. class ChannelGuide(DDBObject):
  53.     ICON_CACHE_SIZES = []
  54.     
  55.     def __init__(self, url, allowedURLs = None):
  56.         checkU(url)
  57.         if allowedURLs is None:
  58.             self.allowedURLs = []
  59.         else:
  60.             self.allowedURLs = allowedURLs
  61.         self.url = url
  62.         self.updated_url = url
  63.         self.title = None
  64.         self.lastVisitedURL = None
  65.         self.iconCache = iconcache.IconCache(self, is_vital = True)
  66.         self.favicon = None
  67.         self.firstTime = True
  68.         if url:
  69.             self.historyLocation = 0
  70.             self.history = [
  71.                 self.url]
  72.         else:
  73.             self.historyLocation = None
  74.             self.history = []
  75.         DDBObject.__init__(self)
  76.         self.downloadGuide()
  77.  
  78.     
  79.     def onRestore(self):
  80.         self.lastVisitedURL = None
  81.         self.historyLocation = None
  82.         self.history = []
  83.         if self.iconCache == None:
  84.             self.iconCache = iconcache.IconCache(self, is_vital = True)
  85.         else:
  86.             self.iconCache.dbItem = self
  87.             self.iconCache.requestUpdate(True)
  88.         if self.getDefault():
  89.             self.allowedURLs = config.get(prefs.CHANNEL_GUIDE_ALLOWED_URLS).split()
  90.             self.allowedURLs.append(config.get(prefs.CHANNEL_GUIDE_FIRST_TIME_URL))
  91.         else:
  92.             self.allowedURLs = []
  93.         self.downloadGuide()
  94.  
  95.     
  96.     def __str__(self):
  97.         return 'Miro Guide <%s>' % (self.url,)
  98.  
  99.     
  100.     def makeContextMenu(self, templateName, view):
  101.         menuItems = [
  102.             ((lambda : app.delegate.copyTextToClipboard(self.getURL())), _('Copy URL to clipboard'))]
  103.         if not self.getDefault():
  104.             i = ((lambda : app.controller.removeGuide(self)), _('Remove'))
  105.             menuItems.append(i)
  106.         
  107.         return menu.makeMenu(menuItems)
  108.  
  109.     
  110.     def remove(self):
  111.         if self.iconCache is not None:
  112.             self.iconCache.remove()
  113.             self.iconCache = None
  114.         
  115.         DDBObject.remove(self)
  116.  
  117.     
  118.     def isPartOfGuide(self, url):
  119.         return isPartOfGuide(url, self.getURL(), self.allowedURLs)
  120.  
  121.     
  122.     def getURL(self):
  123.         return self.url
  124.  
  125.     
  126.     def getFirstURL(self):
  127.         if self.getDefault():
  128.             return config.get(prefs.CHANNEL_GUIDE_FIRST_TIME_URL)
  129.         else:
  130.             return self.url
  131.  
  132.     
  133.     def getLastVisitedURL(self):
  134.         if self.lastVisitedURL is not None:
  135.             logging.info('First URL is %s' % self.lastVisitedURL)
  136.             return self.lastVisitedURL
  137.         elif self.firstTime:
  138.             self.firstTime = False
  139.             logging.info('First URL is %s' % self.getFirstURL())
  140.             return self.getFirstURL()
  141.         else:
  142.             logging.info('First URL is %s' % self.getURL())
  143.             return self.getURL()
  144.  
  145.     
  146.     def getDefault(self):
  147.         return self.url == config.get(prefs.CHANNEL_GUIDE_URL)
  148.  
  149.     
  150.     def getTitle(self):
  151.         if self.title:
  152.             return self.title
  153.         else:
  154.             return self.getURL()
  155.  
  156.     getTitle = returnsUnicode(getTitle)
  157.     
  158.     def guideDownloaded(self, info):
  159.         self.updated_url = unicode(info['updated-url'])
  160.         
  161.         try:
  162.             parser = GuideHTMLParser(self.updated_url)
  163.             parser.feed(info['body'])
  164.             parser.close()
  165.         except:
  166.             pass
  167.  
  168.         self.title = unicode(parser.title)
  169.         self.favicon = unicode(parser.favicon)
  170.         self.extendHistory(self.updated_url)
  171.         self.iconCache.requestUpdate()
  172.         self.signalChange()
  173.  
  174.     
  175.     def guideError(self, error):
  176.         pass
  177.  
  178.     
  179.     def downloadGuide(self):
  180.         httpclient.grabURL(self.getURL(), self.guideDownloaded, self.guideError)
  181.  
  182.     
  183.     def getIconURL(self):
  184.         if self.iconCache.isValid():
  185.             path = self.iconCache.getResizedFilename(20, 20)
  186.             return resources.absoluteUrl(path)
  187.         else:
  188.             return resources.url('images/channelguide-icon-tablist.png')
  189.  
  190.     getIconURL = returnsUnicode(getIconURL)
  191.     
  192.     def getThumbnailURL(self):
  193.         if self.favicon:
  194.             return self.favicon
  195.         elif self.updated_url:
  196.             parsed = urlparse(self.updated_url)
  197.         else:
  198.             parsed = urlparse(self.getURL())
  199.         return parsed[0] + u'://' + parsed[1] + u'/favicon.ico'
  200.  
  201.     
  202.     def extendHistory(self, url):
  203.         if self.historyLocation is None:
  204.             self.historyLocation = 0
  205.             self.history = [
  206.                 url]
  207.         elif self.history[self.historyLocation] == url:
  208.             return None
  209.         
  210.         if self.historyLocation != len(self.history) - 1:
  211.             self.history = self.history[:self.historyLocation + 1]
  212.         
  213.         self.history.append(url)
  214.         self.historyLocation += 1
  215.  
  216.     
  217.     def getHistoryURL(self, direction):
  218.         if direction is not None:
  219.             location = self.historyLocation + direction
  220.             if location < 0:
  221.                 return None
  222.             elif location >= len(self.history):
  223.                 return None
  224.             
  225.         else:
  226.             location = 0
  227.         self.historyLocation = location
  228.         return self.history[self.historyLocation]
  229.  
  230.  
  231.  
  232. class GuideHTMLParser(HTMLParser):
  233.     
  234.     def __init__(self, url):
  235.         self.title = None
  236.         self.in_title = False
  237.         self.baseurl = url
  238.         self.favicon = None
  239.         HTMLParser.__init__(self)
  240.  
  241.     
  242.     def handle_starttag(self, tag, attrs):
  243.         attrdict = { }
  244.         for key, value in attrs:
  245.             attrdict[key] = value
  246.         
  247.         if tag == 'title' and self.title == None:
  248.             self.in_title = True
  249.             self.title = u''
  250.         
  251.         if tag == 'link' and attrdict.has_key('rel') and attrdict.has_key('type') and attrdict.has_key('href') and 'icon' in attrdict['rel'].split(' ') and attrdict['type'].startswith('image/'):
  252.             self.favicon = urljoin(self.baseurl, attrdict['href']).decode('ascii', 'ignore')
  253.         
  254.  
  255.     
  256.     def handle_data(self, data):
  257.         if self.in_title:
  258.             self.title += data
  259.         
  260.  
  261.     
  262.     def handle_endtag(self, tag):
  263.         if tag == 'title' and self.in_title:
  264.             self.in_title = False
  265.         
  266.  
  267.  
  268.  
  269. def getGuideByURL(url):
  270.     return views.guides.getItemWithIndex(indexes.guidesByURL, url)
  271.  
  272.